package gogogo.liwushuo.fragment;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import gogogo.liwushuo.R;
import gogogo.liwushuo.activity.DetailActivity;
import gogogo.liwushuo.activity.HomeAuthorctivity;
import gogogo.liwushuo.adapter.HotAdapter;
import gogogo.liwushuo.bean.HotFragmentBean;
import gogogo.liwushuo.constant.Resource;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class SelectorFragment extends Fragment {

    @InjectView(R.id.hot_tl)
    Toolbar hotTl;
    private List<HotFragmentBean.DataBean.ItemsBean> mDatas;
    private Activity mActivity;
    @InjectView(R.id.hot_rv)
    RecyclerView hotRv;
    private HotAdapter mAdapter;

    public SelectorFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity=getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_selector, container, false);
        ButterKnife.inject(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mDatas=new ArrayList<>();
        mAdapter = new HotAdapter(mActivity, mDatas);
        hotRv.setAdapter(mAdapter);
        hotRv.setLayoutManager(new GridLayoutManager(mActivity,2));
        setData();
        mAdapter.setOnItemClickListener(new HotAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int pos, View itemView) {
                startActivity(new Intent(mActivity, DetailActivity.class));
            }
        });
    }

    private void setData() {
        OkHttpClient okHttpClient=new OkHttpClient();
        Request request=new Request.Builder()
                .url(Resource.URL_SELECT)
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String result = response.body().string();
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        HotFragmentBean bean = new Gson().fromJson(result, HotFragmentBean.class);
                        mDatas.addAll(bean.getData().getItems());
                        mAdapter.notifyDataSetChanged();
                    }
                });
            }
        });
    }
    Handler mHandler=new Handler();
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }


}
